package dk.soegaard.simpleuddibrowser.transport;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;

import org.uddi.api_v3.UDDIInquirySoapService;

public class Inquery {
	private Service service;
	private String portTypeName = "UDDI_Inquiry_PortType";
	private String endpointAddress = "http://discoverybackup.uddi.ehandel.gov.dk/uddi/services/inquiry";
	
	public Inquery() {
		service = new UDDIInquirySoapService();
	}
	
    public String invoke(String request) throws TransformerException  {
    	StringReader stringReader = new StringReader(request);
    	StreamSource requestSource = new StreamSource(stringReader);
    	
    	Source result = invoke(requestSource);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        OutputStream out = new ByteArrayOutputStream();
        StreamResult streamResult = new StreamResult();
        streamResult.setOutputStream(out);
        transformer.transform(result, streamResult);
    	
        return streamResult.getOutputStream().toString();
    }
    
    
    public void invoke(InputStream requestStream, OutputStream resultStream, Transformer transformer) throws TransformerException {
    	StreamSource request = new StreamSource(requestStream);
    	Source result = invoke((Source)request);
        StreamResult streamResult = new StreamResult();
        streamResult.setOutputStream(resultStream);
        transformer.transform(result, streamResult);
    }
    
    public void invoke(InputStream requestStream, OutputStream resultStream) throws TransformerException {
    	StreamSource request = new StreamSource(requestStream);
    	Source result = invoke((Source)request);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        StreamResult streamResult = new StreamResult();
        streamResult.setOutputStream(resultStream);
        transformer.transform(result, streamResult);
    }

    public Source invoke(Source requestSource) {
    	Dispatch<Source> sourceDispatch = null;
        QName serviceQName = service.getServiceName();
        QName portQName = new QName(serviceQName.getNamespaceURI(), portTypeName);
        
        sourceDispatch = service.createDispatch(portQName, Source.class, Service.Mode.PAYLOAD);
        Map<String, Object> requestContext = sourceDispatch.getRequestContext();
        requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointAddress);
        return sourceDispatch.invoke(requestSource);
    }
}
